package com.magic.himalaya.adapters;

import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.himalaya.R;
import com.magic.himalaya.base.BaseApplication;
import com.magic.himalaya.utils.Constants;
import com.magic.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/25.
 */
public class PlayerListAdapter extends RecyclerView.Adapter<PlayerListAdapter.ItemHolder> {
    private static final String TAG = PlayerListAdapter.class.getSimpleName();
    private List<Track> mTrackList = new ArrayList<>();//播放列表的声音
    private int mIndex = -1;

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popupwindow_player_list, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        holder.itemView.setTag(position);
        View itemView = holder.itemView;
        Track track = mTrackList.get(position);

        //获得组件
        TextView title = itemView.findViewById(R.id.track_title);//标题
        ImageView download = itemView.findViewById(R.id.track_download);//下载图标
        View linearLayout = itemView.findViewById(R.id.root_view_line);
        //设置是否正在播放的声音的样式
        title.setTextColor(mIndex == position ?
                BaseApplication.getsAppContext().getColor(R.color.main_color_1) : BaseApplication.getsAppContext().getColor(R.color.white_6));

        title.setCompoundDrawablesWithIntrinsicBounds(mIndex == position ?
                BaseApplication.getsAppContext().getDrawable(R.drawable.ic_player_playing) :
                null, null, null, null);
        title.setCompoundDrawablePadding(5);
        //设置数据
        title.setText(track.getTrackTitle());


        //设置点击事件,当前正在播放的无需设置按钮事件
        if (mOnClickCallBack != null) {
            itemView.setOnClickListener(mIndex == position ?
                    null :
                    v -> {//
                        mOnClickCallBack.onClickListener(position);
                        setIndex(position);
                    });
        }
        //设置点击时的背景变换，当前正在播放的无需设置按钮事件
        linearLayout.setBackground(mIndex == position ?
                BaseApplication.getsAppContext().getDrawable(R.color.white) :
                BaseApplication.getsAppContext().getDrawable(R.drawable.selector_click_background_dark));

    }

    @Override
    public int getItemCount() {
        return mTrackList.size();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //------------------- 以下方法涉及到外部传入数据|设置数据

    /**
     * 添加播放列表声音
     *
     * @param trackList
     */
    public void setData(List<Track> trackList) {
        if (trackList != null) {
            mTrackList.clear();
            mTrackList.addAll(trackList);
        }
        notifyDataSetChanged();
    }

    /**
     * 清除列表声音
     */
    public void clearData() {
        mTrackList.clear();
        notifyDataSetChanged();
    }

    public void setIndex(int index) {
        mIndex = index;
        notifyDataSetChanged();//设置播放位置后进行重新渲染
    }

    // ============== 以下为接口回调
    public interface OnClickCallBack {
        void onClickListener(int position);
    }

    private OnClickCallBack mOnClickCallBack;

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        mOnClickCallBack = onClickCallBack;
    }
}

